<?php

/**
 * Created by JetBrains PhpStorm.
 * User: root
 * Date: 10/4/12
 * Time: 11:41 PM
 * To change this template use File | Settings | File Templates.
 */
abstract class Router_Abstract implements Router_Interface
{

    protected $uri;
    protected $module;
    protected $controller;
    protected $action;
    protected $routes;

    public function __construct()
    {
        $this->uri = Util::getUrl('page');

        $this->module = Config::get('default_module');

        $this->controller = Config::get('default_controller');

        $this->action = Config::get('default_action');

    }

    public function setAction($action)
    {
        $this->action = $action;

    }

    public function getAction()
    {
        return $this->action;

    }

    public function setController($controller)
    {
        $this->controller = $controller;

    }

    public function getController()
    {
        return $this->controller;

    }

    public function setModule($module)
    {
        $this->module = $module;

    }

    public function getModule()
    {
        return $this->module;

    }

    public function setUri($uri)
    {
        $this->uri = $uri;

    }

    public function getUri()
    {
        return $this->uri;

    }

    public static function getInstance($router, $module = '', $controller = '', $action = '')
    {
        if (class_exists($router))
        {
            return new $router($module, $controller, $action);
        }
    }

    public function process()
    {
        //do something/
		
        $this->processRoute();
		
        $this->findConfigRoutes();
		
        $this->filterString();
		
        return $this;

    }

    public function getRoutes()
    {
        return $this->routes;

    }

    public function addRoute(Router_Route $route)
    {
        $this->routes[] = $route;

    }

    public function findConfigRoutes()
    {

        if (is_array($this->routes) && count($this->routes) > 0)
        {
            foreach ($this->routes as $route)
            {

                if ($route->isMatch())
                {

                    $this->module = $route->getModule();

                    $this->controller = $route->getController();

                    $this->action = $route->getAction();
                }
            }
        }

    }

    public function output()
    {

        $controllerClass = $this->getModule() . '_Controller' . Util::appSection() . '_' . $this->getController();

        $controllerClass = new $controllerClass();
      
        $action = $this->action;
        
        return $controllerClass->$action();

    }

    abstract public function processRoute();

    protected function filterString()
    {

        $this->module = ucfirst($this->module);

        $this->controller = ucfirst($this->controller);

        $this->action = strtolower($this->action);

        if (class_exists($this->getModule() . '_Controller' . Util::appSection() . '_' . $this->controller))
        {

            return $this;
        } else
        {

            throw new Exception('Module Controller NOT Found!' . $this->getModule() . '_Controller' . Util::appSection() . '_' . $this->controller);
        }

    }

    public static function getUrlPostfix($returnArray = FALSE)
    {
        if (!Config::get('app.section') || Config::get('app.section') == '')
        {
            $urlPostfix = str_replace(Util::getUrl(), '', Util::getUrl('page'));
        } else
        {
            $urlPostfix = str_replace(Config::get('app.url') . '/' . Config::get('app.section'), '', Util::getUrl('page'));
        }

        if ($returnArray)
        {

            return explode('/', trim($urlPostfix, '/'));
        }

        return trim($urlPostfix, '/');

    }

    public static function getRouter($router)
    {
        if (class_exists($router))
        {
            return new $router;
        }

    }

}